<template>
  <div>
    <GlobalTop></GlobalTop>
    <!--广告-->
    <div class="advertisement">
      <img
        src="https://p6.music.126.net/obj/wonDlsKUwrLClGjCm8Kx/36432764871/a95c/6779/3ffc/662e9e1ad9df00a06e3cc408a826b6ab.png"
        alt=""
      />
    </div>
    <!--编辑推荐-->
    <div class="recommend">
      <div class="title">编辑推荐</div>
    </div>
    <!--加载中...-->
    <GlobalLoading v-if="isShowLoading1"></GlobalLoading>
    <div class="recommend_list">
      <div
        class="block"
        :class="{ actived: currentIndex == index }"
        @click="musicList(index)"
        v-for="(item, index) in recommendList"
        :key="item.recommendId"
      >
        <div class="topShow">
          <div class="listener">
            <van-icon name="eye-o" size="16px" />
            <!--保留两位小数-->
            {{ (item.playCount / 10000).toFixed(2) }}万
          </div>
        </div>
        <img :src="item.picUrl" />
        <div class="introduce">{{ item.name }}</div>
      </div>
    </div>

    <!--最新音乐-->
    <div class="newest">
      <div class="title">最新音乐</div>
    </div>
    <!--加载中...-->
    <GlobalLoading v-if="isShowLoading2"></GlobalLoading>
    <div class="newest_list">
      <GlobalSongs :blockList="blockList"></GlobalSongs>
    </div>
  </div>
</template>
  
<script setup>
import GlobalTop from "@/components/GlobalTop.vue";
import GlobalSongs from "@/components/GlobalSongs.vue";
import GlobalTabbar from "@/components/GlobalTabbar.vue";
import GlobalLoading from "@/components/GlobalLoading.vue";
import { reactive, onMounted, ref } from "vue";
import { showToast } from "vant";
import { useMainStore } from "@/pinia/store.ts";
import router from "@/router/routes";
import { useRoute } from "vue-router";
import axios from "axios";
import { getRecommendListApi, getNewsongApi } from "@/request/api/home.ts";

const currentIndex = ref(0);
const mainStore = useMainStore();
const route = useRoute();
const recommendList = ref([]);
const blockList = ref([]);
const value = ref("");
const isShowLoading1 = ref([]);
const isShowLoading2 = ref([]);

// onMounted(() => {
// 调网易云API接口
//   axios.get('http://localhost:3000/personalized?limit=6').then((res)=>{
//     console.log(res);
//     console.log("编辑推荐");
//     recommendList.value = res.data.result;
//     console.log(recommendList.value);
//   })
// });

// 编辑推荐
const getRecommend = async () => {
  const res = await getRecommendListApi();
  console.log("编辑推荐");
  console.log(res);
  recommendList.value = res.data.result;
  console.log(recommendList.value);
};
onMounted(async () => {
  await getRecommend();
  isShowLoading1.value =false;
});

// 最新音乐
const getNewsong = async () => {
  const res = await getNewsongApi({});
  console.log("最新音乐");
  console.log(res);
  blockList.value = res.data.result;
  console.log(blockList.value);
};
onMounted(async () => {
  await getNewsong();
  isShowLoading2.value =false;
});

// 跳转到音乐列表页
const musicList = (index) => {
  console.log("跳转详情页");
  router.push(`/musicList?id=${recommendList.value[index].id}`);
  console.log(recommendList.value[index].id)
};

</script>
  
<style lang="scss" scoped>
.advertisement {
  width: 100%;
  height: 60px;
  img {
    width: 100%;
    height: 100%;
  }
}
.recommend {
  height: 30px;
  display: flex;
  align-items: center;
  margin: 10px 0;
  .title {
    color: #333;
    font-size: 20px;
    font-weight: 700;
    display: flex;
    align-items: center;
    height: 20px;
    padding-left: 10px;
    margin-left: 5px;
    border-left: 3px solid #d43c33;
  }
}
.recommend_list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  margin-bottom: 12px;
  padding: 0 8px;
  .block {
    position: relative;
    display: block;
    width: 32%;
    margin-bottom: 18px;
    .topShow {
      position: absolute;
      top: 0;
      right: 0;
      background-image: linear-gradient(90deg, #0000, #0004);
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-end;
      .listener {
        padding: 3px;
        color: #fff;
        font-size: 13px;
      }
    }
    img {
      display: flex;
      width: 100%;
      height: 74%;
    }
    .introduce {
      color: #333;
      font-size: 13px;
      display: block;
      margin: 0 5px;
      overflow: hidden;
      line-height: 1.5;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
    }
  }
}

.newest {
  height: 30px;
  display: flex;
  align-items: center;
  margin: 10px 0;
  .title {
    color: #333;
    font-size: 20px;
    font-weight: 700;
    display: flex;
    align-items: center;
    height: 20px;
    padding-left: 10px;
    margin-left: 5px;
    border-left: 3px solid #d43c33;
  }
}
.newest_list {
  display: block;
  padding: 0 15px;
}
</style>